<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        姓:<input type="text" v-model="firstName"></br>
        名:<input type="text" v-model="lastName"></br>
        <p>姓名:{{fullName}}</p>
        姓名:<input type="text" v-model="fullName">
    </div>
    <script src="./js/vue.js"></script>
    <script>
        const vm = new Vue({
            data: {
                firstName: "",
                lastName: "",

            },
            computed: {
                // fullName() {
                //     return this.firstName + this.lastName;
                // }
                //可读可写的计算属性
                fullName: {
                    get() {
                        console.log("get()");
                        if (this.firstName === "" && this.lastName === "") {
                            return "";
                        }
                        if (this.firstName !== "" && this.lastName === "") {
                            return this.firstName;
                        }
                        return this.firstName + " " + this.lastName;
                    },
                    set(val) {
                        console.log("set()", val);
                        const [firstName, lastName] = val.split(" ");

                        this.firstName = firstName;
                        this.lastName = lastName;
                    }
                }
            }
        });
        vm.$mount("#app");
    </script>
</body>

</html>